package org.jeecg.modules.analysis.graph.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.analysis.graph.entity.Graph;
import org.jeecg.modules.analysis.graph.service.IGraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 专利图谱Controller
 * @Author: jeecg-boot
 * @Date: 2025-01-10
 */
@Tag(name = "专利图谱")
@RestController
@RequestMapping("/analysis/graph")
@Slf4j
public class GraphController {

    @Autowired
    private IGraphService graphService;

    /**
     * 根据核心专利查询关联图谱
     * @param patent_id 核心专利ID
     * @param type 关联类型: institution(申请机构), inventor(发明人), ipc(IPC分类), technology(技术领域)
     * @param maxNodes 最大节点数，默认20
     * @return 图谱节点列表
     */
    @AutoLog(value = "专利图谱-根据核心专利查询关联")
    @Operation(summary = "专利图谱-根据核心专利查询关联")
    @GetMapping(value = "/getGraphList")
    public Result<List<Graph>> getGraphList(
            @RequestParam(required = true) String patent_id,
            @RequestParam(required = true) String type,
            @RequestParam(required = false, defaultValue = "20") Integer maxNodes) {

        log.info("查询专利图谱: patent_id={}, type={}, maxNodes={}", patent_id, type, maxNodes);

        try {
            List<Graph> graphList = graphService.getGraphList(patent_id, type, maxNodes);

            if (graphList == null || graphList.isEmpty()) {
                return Result.error("未找到对应数据");
            }

            log.info("成功返回图谱数据: {} 个节点", graphList.size());
            return Result.OK(graphList);

        } catch (Exception e) {
            log.error("查询专利图谱失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}
